/**
 * ECP Web开发日期组合控件
 * Copyright(c) 2012-2020 YGSoft.Inc
 * author:wugang
 * date:2016/6/1
 */

define(['jquery','datepicker'],function($){
	(function($){
		$.ecpRangePicker = function(options){
			var now = new Date();
			//初始日期
			var initStartDate, initEndDate;
			initStartDate = initEndDate = new Date(now.getFullYear(), now.getMonth(),now.getDate());
			var defaults = {
				pickers :[],
				pickersOptions:[],
				initDate:[initStartDate,initEndDate]
			};
			var options = $.extend(defaults,options);
			if(options.pickers.length !== 2) {return;}
			
			var $startPicker = options.pickers[0],
				$endPicker = options.pickers[1],
				startPickerOptions = options.pickersOptions[0],
				endPickerOptions = options.pickersOptions[1];
			
			(typeof startPickerOptions === 'undefined') && (startPickerOptions = {}) ;
			(typeof endPickerOptions === 'undefined') && (endPickerOptions = {});
			//设置自动关闭
			(typeof startPickerOptions.autoclose === 'undefined') && (startPickerOptions.autoclose = true);
			(typeof endPickerOptions.autoclose === 'undefined') && (endPickerOptions.autoclose = true);
			
			$startPicker.val('');
			$endPicker.val('');
			//渲染
			$startPicker.datepicker(startPickerOptions);
			$endPicker.datepicker(endPickerOptions);
		
			//设置初始日期
			(typeof options.initDate[0] !== 'undefined') && ($startPicker.datepicker('setDate', options.initDate[0])) && ($endPicker.datepicker('setStartDate', options.initDate[0]));
			(typeof options.initDate[1] !== 'undefined') && ($endPicker.datepicker('setDate', options.initDate[1]));
			
			$startPicker.on('hide', function(){
				if($startPicker.val() !== ''){
					var date = $startPicker.datepicker('getDate');
					//设置结束日期的startDate并显示出来
					$endPicker.datepicker('setStartDate', date);
					$endPicker.datepicker('show');
				}
			});
			//在开始结束日期都未给定初始值时,不控制先选结束日期
		}
	})(window.jQuery);
	return {
		render : $.ecpRangePicker
	}
});